package com.zl.learning.excel;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;

public class ExcelTest {


    @Test
    public void test2() throws IOException, InvalidFormatException {
        Workbook wb = new XSSFWorkbook(Paths.get("C:\\Users\\zhaolei\\Desktop", "IMSI批量导入Excel模板.xlsx").toFile());
        Sheet sheet = wb.getSheetAt(0);
        int rowNum = sheet.getLastRowNum() + 1;
        for (int i = 0; i < rowNum; i++) {
            Row row = sheet.getRow(i);
            Cell cell = row.getCell(7);
            cell.setCellType(CellType.STRING);
            System.out.println(cell.getStringCellValue());
        }
    }

    @Test
    public void test3() throws IOException, InvalidFormatException {
        Map<String, Object> env = new HashMap<>();
        env.put("encoding", "GBK");
        Path srcPath = Paths.get("C:\\Users\\zhaolei\\Desktop", "IMSI批量导入Excel模板.zip");
        URI uri = URI.create("jar:file:" + srcPath.toUri().getPath());
        try (FileSystem fs = FileSystems.newFileSystem(uri, env);) {
            Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.toString();
                    String filePath = file.getParent().toString();
                    if (fileName.endsWith(".xlsx")) {
                        try (InputStream in = Files.newInputStream(file)) {
                            Workbook wb = new XSSFWorkbook(in);
                            Sheet sheet = wb.getSheetAt(0);
                            int rowNum = sheet.getLastRowNum();
                            for (int i = 0; i < rowNum; i++) {
                                Row row = sheet.getRow(i);
                                System.out.println(row.getCell(0));
                            }
                        }
                    }
                    if (filePath.contains("人脸")) {

                    }
                    System.out.println(fileName);
                    System.out.println(filePath);
                    return super.visitFile(file, attrs);
                }
            });

        }

    }
}
